package com.hjm.project.merchantInfo.merchantRate.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson.JSONObject;
import com.hjm.common.api.cq.PublicJgRequest;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.config.CustomConfig;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.merchantInfo.merchantInfo.domain.MerchantInfo;
import com.hjm.project.merchantInfo.merchantInfo.mapper.MerchantInfoMapper;
import com.hjm.project.merchantInfo.merchantInfo.util.MerchantCommon;
import com.hjm.project.merchantInfo.merchantInfo.util.MerchantRateUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.merchantInfo.merchantRate.mapper.MerchantRateMapper;
import com.hjm.project.merchantInfo.merchantRate.domain.MerchantRate;
import com.hjm.project.merchantInfo.merchantRate.service.IMerchantRateService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;

/**
 * 商户费率绑定Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-04-15
 */
@Service
public class MerchantRateServiceImpl implements IMerchantRateService {
    @Resource
    private MerchantRateMapper merchantRateMapper;
    @Resource
    private MerchantInfoMapper merchantInfoMapper;

    /**
     * 查询商户费率绑定
     * 
     * @param id 商户费率绑定ID
     * @return 商户费率绑定
     */
    @Override
    public MerchantRate selectMerchantRateById(Integer id) {
        return merchantRateMapper.selectMerchantRateById(id);
    }

    /**
     * 查询商户费率绑定列表
     * 
     * @param merchantRate 商户费率绑定
     * @return 商户费率绑定
     */
    @Override
    public List<MerchantRate> selectMerchantRateList(MerchantRate merchantRate) {
        return merchantRateMapper.selectMerchantRateList(merchantRate);
    }

    /**
     * 查询商户费率绑定列表
     *
     * @param merchantRate 商户费率绑定
     * @return 商户费率绑定 一个结果
     */
    @Override
    public MerchantRate selectMerchantRateOne(MerchantRate merchantRate) {
        return merchantRateMapper.selectMerchantRateOne(merchantRate);
    }

    /**
     * 新增商户费率绑定
     * 
     * @param merchantRate 商户费率绑定
     * @return 结果
     */
    @Override
    public int insertMerchantRate(MerchantRate merchantRate) {
        return merchantRateMapper.insertMerchantRate(merchantRate);
    }

    /**
     * 修改商户费率绑定
     * 
     * @param merchantRate 商户费率绑定
     * @return 结果
     */
    @Override
    public int updateMerchantRate(MerchantRate merchantRate) {
        return merchantRateMapper.updateMerchantRate(merchantRate);
    }

    /**
     * 删除商户费率绑定对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerchantRateByIds(String ids) {
        return merchantRateMapper.deleteMerchantRateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户费率绑定信息
     * 
     * @param id 商户费率绑定ID
     * @return 结果
     */
    @Override
    public int deleteMerchantRateById(Integer id) {
        return merchantRateMapper.deleteMerchantRateById(id);
    }

    @Override
    @Transactional
    public AjaxResult updateMerchantRate(MerchantCommon merchantCommon) {

        String fj = merchantCommon.getS0_shuakaDaijikaFj().toString();
        String merno = merchantCommon.getMerno();

        MerchantRate rate = new MerchantRate();
        rate.setMerno(merno);
        List<MerchantRate> oldRateList = merchantRateMapper.selectMerchantRateList(rate);

        //刷卡借记卡费率
        MerchantRate mr = new MerchantRate();
        mr.setMerno(merno);
        mr.setRateAffix(fj);
        mr.setCardType(1);
        mr.setRateType(0);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getS0_shuakaJiejika().toString());
        merchantRateMapper.updateMerchantRate(mr);
        mr.setRateCode(merchantCommon.getS0_shuakaJiejika().toString());
        //刷卡借记卡封顶
        mr.setIsFd(1);
        mr.setRateCode(merchantCommon.getS0_shuakaJiejikaFd().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //刷卡贷记卡
        mr.setCardType(2);
        mr.setRateType(0);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getS0_shuakaDaijika().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //银联二维码借记卡
        mr.setCardType(1);
        mr.setRateType(4);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getS0_qrJiejika().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //银联二维码贷记卡
        mr.setCardType(2);
        mr.setRateType(4);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getS0_qrDaijika().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //云闪付借记卡
        mr.setCardType(1);
        mr.setRateType(1);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getYunshanfu0().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //云闪付贷记卡
        mr.setCardType(2);
        mr.setRateType(1);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getYunshanfu0().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //微信
        mr.setCardType(2);
        mr.setRateType(2);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getSaoma0Wx().toString());
        merchantRateMapper.updateMerchantRate(mr);
        //支付宝
        mr.setCardType(2);
        mr.setRateType(3);
        mr.setIsFd(0);
        mr.setRateCode(merchantCommon.getSaoma0Zfb().toString());
        merchantRateMapper.updateMerchantRate(mr);

        MerchantInfo merchantInfo = merchantInfoMapper.selectMerchantInfoByMerNo(merno);
        //请求变更
        JSONObject rateBody = MerchantRateUtils.getReqBody(merchantCommon);
        JSONObject reqBody = new JSONObject();
        reqBody.put("orgMerNo",merchantInfo.getLdMerno());
        reqBody.put("orgSerial",merchantInfo.getOrgMerno());
        reqBody.put("relationNum",merchantInfo.getOrgSerial());
        reqBody.put("legalCer","1");
        reqBody.put("rateInfo",rateBody);
        Map<String, String> reqData = PublicJgRequest.getReqData(reqBody.toJSONString(), AutoLoadData.updateMerchant);
        if (!"200".equals(reqData.get("statusCode"))) {
            throw new BusinessException(reqData.get("message"));
        }

        List<MerchantRate> newRateList = merchantRateMapper.selectMerchantRateList(rate);

        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                for (MerchantRate merchantRate : oldRateList) {
                    merchantRate.setSource("0");
                    merchantRateMapper.insertMerchantRateRecord(merchantRate);
                }
                for (MerchantRate merchantRate : newRateList) {
                    merchantRate.setSource("1");
                    merchantRateMapper.insertMerchantRateRecord(merchantRate);
                }
            }
        });

        return AjaxResult.success();
    }
}
